package com.zsc.shixun.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * <p>
 * rabbitMQ消息中间件配置
 * </p>
 *
 * @author ZWYZY
 * @since 2020/6/14
 */
@Configuration
public class RabbitConfig {
    @Value("code-queue")
    private String code_queue;
    @Value("notice-queue")
    private String notice_queue;

    /**
     * json对象解析配置bean
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 获取验证码队列bean
     *
     * @return
     */
    @Bean
    public Queue getCodeQueue() {
        return new Queue(code_queue);
    }

    /**
     * 获取通知队列bean
     *
     * @return
     */
    @Bean
    public Queue getNoticeQueue() {
        return new Queue(notice_queue);
    }

    /**
     * 获取验证码消息交换器bean
     *
     * @return
     */
    @Bean
    public Exchange getCodeExchange() {
        return ExchangeBuilder.fanoutExchange("code_exchange").build();
    }

    /**
     * 获取通知消息交换器bean
     *
     * @return
     */
    @Bean
    public Exchange getNoticeExchange() {
        return ExchangeBuilder.fanoutExchange("notice_exchange").build();
    }

    /**
     * 验证码交换器绑定验证码队列
     *
     * @return
     */
    @Bean
    public Binding bindingCode() {
        return BindingBuilder.bind(getCodeQueue()).to(getCodeExchange()).with("").noargs();
    }

    /**
     * 通知交换器绑定通知队列
     *
     * @return
     */
    @Bean
    public Binding bindingNotice() {
        return BindingBuilder.bind(getNoticeQueue()).to(getNoticeExchange()).with("").noargs();
    }

}
